﻿using System;
using SCG.SolarVengeanceEngine;
using System.Collections.Generic;
using SCG.General;

namespace SCG.SolarVengeance.Scenarios
{
    public class HighEnergy : Scenario
    {
        //Enlarge the map
        public override int MapWidth
        {
            get
            {
                return 200;
            }
        }
        public override int MapHeight
        {
            get
            {
                return 200;
            }
        }

        //Process the high energy nebula
        //SV5 assigns terrain characters as 'n' for CreateTerrainImage nebula, and 'N' for normal nebula, we need to look for the 'n'
        public override void ProcessImpulse(int impulse)
        {
            if (impulse == 1)
                DynamicEvents.DisplayMessage("StarShips in the central Nebula slowly increase in power!");

            foreach (StarShip ship in Game.StarShips)
                if (Game.GetTerrain(ship.X, ship.Y) == 'n')
                    if (Random(0, 100) > 90)
                        DynamicEvents.AdjustShipValues(ship, ship.Engines, ship.MaxEngines, ship.Value + 1, ship.MaxValue + 1, true);
        }

        //Build the scenario
        public override void BuildScenario(int numPlayers, int capitalValues, int startingResources)
        {
            //randomized variables
            int numSystems = Random(7, 12);

            //Create the central nebula
            CreateTerrainImage(TerrainType.Nebula, "HighEnergy", 95 + Random(-20, 20), 95 + Random(-20, 20), 0.6);

            //Find cluster location for each player, should be away from other capitals and about 60 away from center
            int x = 0;
            int y = 0;
            List<SCGPoint> capitals = new List<SCGPoint>();
            foreach(Player p in Game.Players)
            {
                bool capLocationOK = false;
                do
                {
                    capLocationOK = false;
                    x = Random(0, 199);
                    y = Random(0, 199);

                    //should be between 60 and 70 away from center
                    double dist = Distance(x, y, 100, 100);
                    if (dist >= 60 && dist <= 70)
                    {
                        capLocationOK = true;

                        //should be at least 30 away from other capitals
                        foreach(SCGPoint pt in capitals)
                            if (Distance(x, y, pt.X, pt.Y) < 30)
                            {
                                capLocationOK = false;
                                break;
                            }
                    }
                }
                while(!capLocationOK);

                //save the location
                capitals.Add(new SCGPoint(x, y));

                //create their cluster
                CreateStarCluster(numSystems, x, y, x, y, 12, 4);

                //Assign capital
                p.Capital = Game.StarSystems[Game.StarSystems.Count - 1];

                //and grant ownership to 2 others
                Game.StarSystems[Game.StarSystems.Count - 2].Owner = p;
                Game.StarSystems[Game.StarSystems.Count - 3].Owner = p;
            }

            //create wormholes from clusters to center, and from center to cluster
            foreach (SCGPoint pt in capitals)
            {
                int x1 = (int)(pt.X + Random(-10, 10));
                int y1 = (int)(pt.Y + Random(-10, 10));
                int x2 = 100 + Random(-20, 20);
                int y2 = 100 + Random(-20, 20);
                CreateWormhole(x1, y1, x2, y2);

                x1 = 100 + Random(-10, 10);
                y1 = 100 + Random(-10, 10);
                x2 = (int)(pt.X + Random(-20, 20));
                y2 = (int)(pt.Y + Random(-20, 20));
                CreateWormhole(x1, y1, x2, y2);
            }
        }

        //Scenario description
        public override string Description
        {
            get
            {
                return "The Star Lords in the Cassiopia Sector discovered a new cosmological phenomenon in their vicinity; " +
                    "a high energy Nebula with a unique property.  StarShips in the Nebula slowly increase in power!  " +
                    "Wormholes lead in and out of this valuable location from each of the Star Lords' clusters.";
            }
        }
    }
}
